(function ($) {
    $.extend(
        {
            CheckMobile: function (mobile) {
                var regMobile1 = /^1[3,4,5,7,8]{1}\d{9}$/;
                if (regMobile1.test(mobile)) {
                    return true;
                }
                else {
                    return false;
                }
            },
            CheckEmail: function (mail) {
                var regMail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
                if (regMail.test(mail)) {
                    return true;
                }
                else {
                    return false;
                }
            },
            RemoveSpecialChar: function (input) {
                input = input.replace(/'/gi, '').replace(/"/gi, '').replace(/</gi, '').replace(/>/gi, '').replace(/\//gi, '').replace(/\\/gi, '').replace(/\./gi, '');
                while (input.indexOf(" ") > -1) {
                    input = input.replace(" ", "");
                }
                while (input.indexOf("&") > -1) {
                    input = input.replace("&", "");
                }
                while (input.indexOf("\\") > -1) {
                    input = input.replace("\\", "");
                }
                return input;
            }
        });
})(jQuery);

(function ($) {
    $.validator = {};
    $.extend($.validator, {
        constructors: {
            ValidateResult: function () {
                this.success = false;
                this.errMsg = "";
            },
            ErrorHandler: function (opts) {
                var defaults = {
                    ctl: null, //需要验证的控件
                    errorTip: "", //验证错误提示
                    style: "", //样式
                    tipDirection: "down", //提示标签显示方向（右，下）
                    pass: false, //验证是否通过
                    callback: null//验证失败回调方法
                };
                this.options = $.extend(defaults, opts);
                if (typeof (this.options.callback) === "function") {
                    this.handle = function () {
                        this.options.callback(this.options.errorTip);
                    };
                } else {
                    this.handle = function () {
                        this.htmlTmp = "<div  class=\"error\">CONTENT</div>";
                        if (this.options.ctl) {
                            var tip = $(".error", this.options.ctl.parent().parent());
                            if (this.options.pass) {
                                if (tip.length > 0) {
                                    tip.hide();
                                }
                            } else {
                                if (tip.length == 0) {
                                    this.htmlTmp = this.htmlTmp.replace("STYLE", this.options.style).replace("CONTENT", this.options.errorTip);
                                    this.options.ctl.parent().parent().append(this.htmlTmp);
                                } else {
                                    $(tip).html(this.options.errorTip);
                                    tip.show();
                                }
                            }
                        }
                    };
                }
            }
        },
        setting: {
            el: null,
            allowEmpty: false, //是否允许为空
            defaultValue: "请输入", //缺省文字
            emptyTip: "请输入", //空值提示
            errorTip: "请输入正确内容", //错误提示
            customCheck: null, //外挂验证逻辑
            failCallback: null, //验证失败回调
            style: "", //附加样式
            tipDirection: "down" //显示方向，右边或者底部
        },
        commonSetting: {
            name: {
                setting: {
                    allowEmpty: false,
                    emptyTip: "姓名不能为空",
                    errorTip: "请输入2-6个汉字"
                },
                method: function () {
                    var _setting = $.validator.setting;
                    var result = new $.validator.createResult();
                    var user = $.RemoveSpecialChar($.trim(_setting.el.val()));
                    if (user === "") {
                        result.success = _setting.allowEmpty;
                        result.errMsg = (_setting.allowEmpty ? "" : _setting.emptyTip);
                    } else {
                        if (user.length > 6 || user.length < 2) {
                            result.errMsg = _setting.errorTip;
                        } else if (!RegExp(/^[\u4e00-\u9fa5\\]+$/).test(user)) {
                            result.errMsg = _setting.errorTip;
                        } else result.success = true;
                    }
                    return result;
                }
            },
            phone: {
                setting: {
                    allowEmpty: false,
                    defaultValue: "请输入手机号",
                    emptyTip: "请输入手机号",
                    errorTip: "请输入正确的手机号码"
                },
                method: function () {
                    var _setting = $.validator.setting;
                    var result = new $.validator.createResult();
                    var phone = $.trim(_setting.el.val());
                    if (phone === "" || phone == _setting.defaultValue) {
                        result.success = _setting.allowEmpty;
                        result.errMsg = (_setting.allowEmpty ? "" : _setting.emptyTip);
                    }
                    else if (!$.CheckMobile(phone)) {
                        result.errMsg = _setting.errorTip;
                    }
                    else result.success = true;
                    return result;
                }
            },

            email: {
                setting: {
                    emptyTip: "邮箱不能为空",
                    errorTip: "请输入正确格式的邮箱"
                },
                method: function () {
                    var _setting = $.validator.setting;
                    var result = new $.validator.createResult();
                    var mail = $.trim(_setting.el.val());
                    if ($.trim(mail) === "" || $.trim(mail) == _setting.defaultValue) {
                        result.success = _setting.allowEmpty;
                        result.errMsg = (_setting.allowEmpty ? "" : _setting.emptyTip);
                    }
                    else if (!$.CheckEmail(mail)) {
                        result.errMsg = _setting.errorTip;
                    } else result.success = true;
                    return result;
                }
            },
            checkcode: {
                setting: {
                    emptyTip: "请输入确认码",
                    source: 0
                },
                method: function () {
                    var _setting = $.validator.setting;
                    var result = new $.validator.createResult();
                    var code = $.trim(_setting.el.val());
                    if ($.trim(code) === "" || $.trim(code) == _setting.emptyTip) {
                        result.errMsg = _setting.emptyTip;
                    }
                    else result.success = true;
                    return result;
                }
            },
            textbox: {
                setting: {
                    allowEmpty: false,
                    defaultValue: "请输入",
                    emptyTip: "请输入"
                },
                method: function () {
                    var _setting = $.validator.setting;
                    var result = new $.validator.createResult();
                    var code = $.trim(_setting.el.val());
                    if ($.trim(code) === "" || $.trim(code) == _setting.defaultValue) {
                        result.success = _setting.allowEmpty;
                        result.errMsg = (_setting.allowEmpty ? "" : _setting.emptyTip);
                    }
                    else result.success = true;
                    return result;
                }
            },
            select: {
                setting: { emptyTip: "请选择" },
                method: function () {
                    var _setting = $.validator.setting;
                    var result = new $.validator.createResult();
                    var code = _setting.el.val();
                    if ($.trim(code) === "") {
                        result.errMsg = _setting.emptyTip;
                    } else result.success = true;
                    return result;
                }
            }
        },
        createResult: function(err) {
            var result= new $.validator.constructors.ValidateResult();
            if (err) {
                 result.errMsg = err;
            }
            return result;
        },
        validate: function (opt, extOpt) {
            if (typeof (event) !== "undefined") {
                try { event.preventDefault(); } catch (e) { }
            }
            var defaults = {
                el: null,
                defaultValue: "请输入",
                emptyTip: "请输入",
                errorTip: "请输入正确内容",
                customCheck: null,
                failCallback: null,
                style: "",
                tipDirection: "down"
            };
            var _self = $.validator;
            $.extend(_self.setting, defaults);
            var result = new _self.constructors.ValidateResult();
            if (typeof opt === "string" && _self.commonSetting[opt]) {
                $.extend(_self.setting, _self.commonSetting[opt].setting);
                if (typeof extOpt === "object") {
                    $.extend(_self.setting, extOpt);
                }
                result = _self.commonSetting[opt].method();
            }
            else if (typeof opt === "object" && typeof opt.customCheck !== "undefined") {
                $.extend(_self.setting, opt);
                if (typeof (_self.setting.customCheck) === "function") {
                    result = _self.setting.customCheck();
                }
            }
            if (result) {
                new _self.constructors.ErrorHandler({
                    ctl: _self.setting.el,
                    errorTip: result.errMsg,
                    style: _self.setting.style,
                    tipDirection: _self.setting.tipDirection,
                    pass: result.success,
                    callback: _self.setting.failCallback
                }).handle();
            }
            return result;
        }
    });
    jQuery.validator.constructors.ValidateResult.prototype.IsPass = function () {
                return this.success;
            };
})(jQuery);